package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eva.epc.common.util.CommonUtils;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.UpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IZhikeGroupBaseService;
import com.mdd.admin.validate.ZhikeGroupBaseCreateValidate;
import com.mdd.admin.validate.ZhikeGroupBaseUpdateValidate;
import com.mdd.admin.validate.ZhikeGroupBaseSearchValidate;
import com.mdd.admin.vo.ZhikeGroupBaseListedVo;
import com.mdd.admin.vo.ZhikeGroupBaseDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ZhikeGroupBase;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.ZhikeGroupBaseMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Pattern;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * 群组基本信息实现类
 * @author LikeAdmin
 */
@Service
public class ZhikeGroupBaseServiceImpl implements IZhikeGroupBaseService {

    @Resource
    ZhikeGroupBaseMapper zhikeGroupBaseMapper;

    /**
     * 群组基本信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ZhikeGroupBaseListedVo>
     */
    @Override
    public PageResult<ZhikeGroupBaseListedVo> list(PageValidate pageValidate, ZhikeGroupBaseSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ZhikeGroupBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("g_id");

        zhikeGroupBaseMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:groupNumber@group_number:str",
            "=:gStatus@g_status:long",
            "like:gName@g_name:str",
        });

        IPage<ZhikeGroupBase> iPage = zhikeGroupBaseMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ZhikeGroupBaseListedVo> list = new LinkedList<>();
        for(ZhikeGroupBase item : iPage.getRecords()) {
            ZhikeGroupBaseListedVo vo = new ZhikeGroupBaseListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 群组基本信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ZhikeGroupBase
     */
    @Override
    public ZhikeGroupBaseDetailVo detail(String id) {
        ZhikeGroupBase model = zhikeGroupBaseMapper.selectOne(
                new QueryWrapper<ZhikeGroupBase>()
                    .eq("g_id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ZhikeGroupBaseDetailVo vo = new ZhikeGroupBaseDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 群组基本信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ZhikeGroupBaseCreateValidate createValidate) {
        ZhikeGroupBase model = new ZhikeGroupBase();
        model.setGroupNumber(createValidate.getGroupNumber());
        model.setGStatus(createValidate.getGStatus());
        model.setGName(createValidate.getGroupName());
        model.setGOwnerUserUid(createValidate.getGroupOwnerUserUid());
        model.setGNotice(createValidate.getGroupNotice());
        model.setMaxMemberCount(createValidate.getMaxMemberCount());
        model.setGMemberCount(createValidate.getGroupMemberCount());
        model.setCreateUserUid(createValidate.getCreateUserUid());
        model.setCreateTime(new Date());
        zhikeGroupBaseMapper.insert(model);
    }

    /**
     * 群组基本信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(UpdateValidate updateValidate) {
        String field = updateValidate.getField();
        String value = updateValidate.getValue();
        ZhikeGroupBase model = zhikeGroupBaseMapper.selectOne(
                new QueryWrapper<ZhikeGroupBase>()
                    .eq("g_id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        switch (field) {
            case "gName":
                model.setGName(value);
                break;
            case "gNotice":
                model.setGNotice(value);
                break;
            default:
                throw new OperateException("不被支持的字段类型!");
        }

        zhikeGroupBaseMapper.updateById(model);

    }

    /**
     * 群组基本信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ZhikeGroupBase model = zhikeGroupBaseMapper.selectOne(
                new QueryWrapper<ZhikeGroupBase>()
                    .eq("g_id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        zhikeGroupBaseMapper.delete(new QueryWrapper<ZhikeGroupBase>().eq("g_id", id));
    }

}
